#ifndef HKMUTICAM
#define HKMUTICAM

#include <stdio.h>
#include <pthread.h>
#include <vector>
#include <array>
#include <opencv2/opencv.hpp>
#include <X11/Xlib.h>
#include "HKCamera/MvErrorDefine.h"
#include "HKCamera/CameraParams.h"
#include "HKCamera/MvCameraControl.h"
#include "HKCamera/PixelType.h"

//********** define ************************************/
#define MAX_IMAGE_DATA_SIZE (4 * 1024 * 1280)
#define CAMERA_NUM 2

struct Camera_Param
{
    int width;
    int height;
    int Offset_x;
    int Offset_y;
    int FrameRateEnable;
    int FrameRate;
    int BurstFrameCount;
    int ExposureTime;
    int GammaEnable;
    float Gamma;
    int GainAuto;
    float Gain;
    int SaturationEnable;
    int Saturation;
    int TriggerMode;
    int TriggerSource;
    int LineSelector;
    Camera_Param()
    {
        width = 1280;
        height = 1024;
        FrameRateEnable = (int)true;
        FrameRate = 210;
        BurstFrameCount = 10; // 一次触发采集的次数
        ExposureTime = 7000;
        GammaEnable = (int)true;
        Gamma = (float)0.7;
        GainAuto = 0;
        Gain =1.2;
        SaturationEnable = (int)true;
        Saturation = 128;
        Offset_x = 0;
        Offset_y = 0;
        TriggerMode = 1;
        TriggerSource = 2;
        LineSelector = 2;
    }
};

class HKMutiCam
{
public:
    //********** mutex ************************************/
    static pthread_mutex_t mutex_;
    //********** frame ************************************/
    static std::array<cv::Mat, CAMERA_NUM> frames_;
    //********** frame_empty ******************************/
    static std::array<bool, CAMERA_NUM> frame_emptys_;
    //********** CameraProperties config ************************************/

    enum CamerProperties
    {
        CAP_PROP_FRAMERATE_ENABLE,  //帧数可调
        CAP_PROP_FRAMERATE,         //帧数
        CAP_PROP_BURSTFRAMECOUNT,   //外部一次触发帧数
        CAP_PROP_HEIGHT,            //图像高度
        CAP_PROP_WIDTH,             //图像宽度
        CAP_PROP_EXPOSURE_TIME,     //曝光时间
        CAP_PROP_GAMMA_ENABLE,      //伽马因子可调
        CAP_PROP_GAMMA,             //伽马因子
        CAP_PROP_GAINAUTO,          //亮度
        CAP_PROP_GAIN,
        CAP_PROP_SATURATION_ENABLE, //饱和度可调
        CAP_PROP_SATURATION,        //饱和度
        CAP_PROP_OFFSETX,           //X偏置
        CAP_PROP_OFFSETY,           //Y偏置
        CAP_PROP_TRIGGER_MODE,      //外部触发
        CAP_PROP_TRIGGER_SOURCE,    //触发源
        CAP_PROP_LINE_SELECTOR      //触发线

    };


    HKMutiCam();
    ~HKMutiCam();


    //********** 原始信息转换线程 **********************/
    static void *HKWorkThread(void *p_handle);
    //********** 输出摄像头信息 ***********************/
    bool PrintDeviceInfo(MV_CC_DEVICE_INFO *pstMVDevInfo);

    //********** 设置摄像头参数 ***********************/
    bool set(int id, HKMutiCam::CamerProperties type, float value);
    //********** 恢复默认参数 *************************/
    bool reset(int id);

    //********** 读图10个相机的原始图像 ********************************/
    void ReadImg(std::vector<cv::Mat> &images);
    void saveImg();


private:
    //********** handle ******************************/
    std::vector<void*> handles;
    //********** nThreadID ******************************/
    std::vector<pthread_t> nThreadIDs;
    //********** yaml config ******************************/
    int nRet;
    Camera_Param camera_param;
    int save_count;
};

#endif // HKMUTICAM

